﻿unit smOrderListFrame;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SystemBaseFrame, Data.DB, Data.Win.ADODB,
  Vcl.ExtCtrls, RzPanel, RzSplit, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, cxStyles,
  dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit,
  cxNavigator, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  Vcl.StdCtrls, DBGridEh, Vcl.Mask, DBCtrlsEh, DBLookupEh, cxContainer, RzLabel,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, RzButton,Datasnap.DBClient,

  PYIndexUnit,System.StrUtils,SK0001Comm,

  CmCustomerFileModel,PmItemOtherModel,PmMsunitModel,PmPackageTypeModel,PmRanksModel,
  SmChargeItemModel,SmInvoiceTypeModel,SmPaywayModel,SmShipTypeModel,
  PmItemDataModel,SmOrderModel,SmOrderMstModel,SmOrderDtlModel,

  SmOrderMstService,CmCustomerFileService,PmItemOtherService,
  PmMsunitService,PmPackageTypeService,PmRanksService,SmChargeItemService,
  SmInvoiceTypeService,SmPaywayService,SmShipTypeService,PmItemDataService,
  SmOrderDtlService,

  DBGridEhGrouping,ToolCtrlsEh, DBGridEhToolCtrls, DynVarsEh, GridsEh, DBAxisGridsEh,
  cxBlobEdit, cxCurrencyEdit, cxSpinEdit, cxCalc, cxGridBandedTableView,
  cxGridDBBandedTableView, System.Actions, Vcl.ActnList, Vcl.ImgList, SkPages, QJson;

type
  TFrame_smOrderList = class(TFrame_SytemBase)
    RzSplitter_Main: TRzSplitter;
    RzSplitter_Seach_List: TRzSplitter;
    RzPanel_Seach: TRzPanel;
    LabelEdit_OrderId: TLabeledEdit;
    cxLookupComboBox_Customer: TcxLookupComboBox;
    RzLabel_CustomerId: TRzLabel;
    cxLookupComboBox_InvoiceType: TcxLookupComboBox;
    RzLabel_InvoiceType: TRzLabel;
    RzLabel_Payway: TRzLabel;
    cxLookupComboBox_Payway: TcxLookupComboBox;
    RzLabel_ShipType: TRzLabel;
    cxLookupComboBox_ShipType: TcxLookupComboBox;
    cxLookupComboBox_EmpId: TcxLookupComboBox;
    RzLabel_EmpId: TRzLabel;
    RzToolBar_Main: TRzToolbar;
    Cds_Customer: TClientDataSet;
    Ds_Customer: TDataSource;
    Ds_ItemOther: TDataSource;
    Cds_ItemOther: TClientDataSet;
    Ds_Msunit: TDataSource;
    Cds_Msunit: TClientDataSet;
    Ds_PackageType: TDataSource;
    Cds_PackageType: TClientDataSet;
    Ds_Ranks: TDataSource;
    Cds_Ranks: TClientDataSet;
    Ds_ChargeItem: TDataSource;
    Cds_ChargeItem: TClientDataSet;
    Ds_InvoiceType: TDataSource;
    Cds_InvoiceType: TClientDataSet;
    Ds_Payway: TDataSource;
    Cds_Payway: TClientDataSet;
    Ds_ShipType: TDataSource;
    Cds_ShipType: TClientDataSet;
    Ds_ItemData: TDataSource;
    Cds_ItemData: TClientDataSet;
    Ds_OrderDtl: TDataSource;
    Cds_OrderDtl: TClientDataSet;
    ImageList_ToolBar: TImageList;
    ActionList_Main: TActionList;
    Action_Add: TAction;
    Action_View: TAction;
    RzToolButton_SaveAndBack: TRzToolButton;
    RzToolButton_SaveAndView: TRzToolButton;
    RzToolButton_Close: TRzToolButton;
    DBGridEh_OrderList: TDBGridEh;
    SkPages1: TSkPages;
    RzPanel_Page: TRzPanel;
    Cds_OrderMst: TClientDataSet;
    Ds_OrderMst: TDataSource;
    RzPanel_OrderDetail: TRzPanel;
    DBGridEh_OrderDetail: TDBGridEh;
    Action_Edit: TAction;
    Action_Delete: TAction;
    RzToolButton_Delete: TRzToolButton;
    procedure cxLookupComboBox_CustomerIdPropertiesChange(Sender: TObject);
    procedure SkPages1PageChange(Sender: TObject; CurrPage, PageSize: Integer;
      var TotalRecordCount: Integer; var IsSuccess: Boolean; var ErrorCode,
      ErrorText: string);
    procedure Action_AddExecute(Sender: TObject);
    procedure Action_ViewExecute(Sender: TObject);
    procedure Action_EditExecute(Sender: TObject);
    procedure Action_DeleteExecute(Sender: TObject);

  private
    { Private declarations }

    fLoginOrgCustomerFileList:TCmCustomerFileList;
    fLoginOrgItemOtherList:TPmItemOtherList;
    fLoginOrgItemDataList:TPmItemDataList;
    fLoginOrgMsunitList:TPmMsunitList;
    fLoginOrgPackageTypeList:TPmPackageTypeList;
    fLoginOrgRanksList:TPmRanksList;
    fLoginOrgChargeItemList:TSmChargeItemList;
    fLoginOrgInvoiceTypeList:TSmInvoiceTypeList;
    fLoginOrgPaywayList:TSmPaywayList;
    fLoginOrgShipTypeList:TSmShipTypeList;

    fOrderService:TSmOrderService;
    fOrderDtlService:TSmOrderDtlService;
    fCustomerFileService:TCmCustomerFileService;
    fItemOtherService:TPmItemOtherService;
    fItemDataService:TPmItemDataService;
    fMsunitService:TPmMsunitService;
    fPackageTypeService:TPmPackageTypeService;
    fRanksService:TPmRanksService;
    fChargeItemService:TSmChargeItemService;
    fInvoiceTypeService:TSmInvoiceTypeService;
    fPaywayService:TSmPaywayService;
    fShipTypeService:TSmShipTypeService;

    fOrderList:TSmOrderList;
    fCurrOrder:TSmOrder;

    function InitSeachPanel(out oError:string):Boolean;
    function InitOrderList(out oError:string):Boolean;

    procedure RefreshComponent(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure RefreshButton(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure RefreshFrame(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure Obj2Frame(const iItem:TSmOrder; const iRunMode:eRunMode);
    procedure Frame2Obj(const iItem:TSmOrder; const iRunMode:eRunMode);

    procedure Ds_OrderMstDataChange(Sender: TObject; Field: TField);

  public
    { Public declarations }
    constructor Create(const iOwner:TWinControl; const iMainWindowHandle:HWND; const iSKSOFT,iSK0001:TAdoConnection); overload;
    destructor Destroy();override;

    procedure InitFrame(const iManageKey:string; const iRunMode:eRunMode; const iBillNo:string; const iParamStr:string);override;
  end;

  function GetInstance(const iApplicationHandle:HWND; const iParentWinControlPtr:Integer; const iMainWindowHandle:HWND; const iSKSOFT,iSK0001:TAdoConnection):TFrame_SytemBase;stdcall;

var
  Frame_smOrderList: TFrame_smOrderList;

implementation

{$R *.dfm}

function GetInstance(const iApplicationHandle:HWND; const iParentWinControlPtr:Integer; const iMainWindowHandle:HWND; const iSKSOFT,iSK0001:TAdoConnection):TFrame_SytemBase;stdcall;
begin
  Application.Handle:=iApplicationHandle;
  Result:=TFrame_smOrderList.Create(TWinControl(Pointer(iParentWinControlPtr)),iMainWindowHandle,iSKSOFT,iSK0001);
end;

constructor TFrame_smOrderList.Create(const iOwner:TWinControl; const iMainWindowHandle:HWND; const iSKSOFT,iSK0001:TAdoConnection);
begin
  inherited Create(iOwner,iMainWindowHandle,iSKSOFT,iSK0001);
  Self.Parent:=iOwner;
  Self.Align:=alClient;

  fLoginOrgCustomerFileList:=TCmCustomerFileList.Create;
  fLoginOrgItemOtherList:=TPmItemOtherList.Create;
  fLoginOrgItemDataList:=TPmItemDataList.Create;
  fLoginOrgMsunitList:=TPmMsunitList.Create;
  fLoginOrgPackageTypeList:=TPmPackageTypeList.Create;
  fLoginOrgRanksList:=TPmRanksList.Create;
  fLoginOrgChargeItemList:=TSmChargeItemList.Create;
  fLoginOrgInvoiceTypeList:=TSmInvoiceTypeList.Create;
  fLoginOrgPaywayList:=TSmPaywayList.Create;
  fLoginOrgShipTypeList:=TSmShipTypeList.Create;

  fOrderList:=TSmOrderList.Create;

  fOrderService:=TSmOrderService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fOrderDtlService:=TSmOrderDtlService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fCustomerFileService:=TCmCustomerFileService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fItemOtherService:=TPmItemOtherService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fItemDataService:=TPmItemDataService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fMsunitService:=TPmMsunitService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fPackageTypeService:=TPmPackageTypeService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fRanksService:=TPmRanksService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fChargeItemService:=TSmChargeItemService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fInvoiceTypeService:=TSmInvoiceTypeService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fPaywayService:=TSmPaywayService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);
  fShipTypeService:=TSmShipTypeService.Create(fSK0001Conn,fSK0001Conn,EmptyStr);

  fRunMode:=rmView;
end;

procedure TFrame_smOrderList.cxLookupComboBox_CustomerIdPropertiesChange(
  Sender: TObject);
begin
  inherited;
//   ShowMessage(cxLookupComboBox_CustomerId.EditValue+':'+cxLookupComboBox_CustomerId.EditText);
end;

destructor TFrame_smOrderList.Destroy();
begin
  fLoginOrgCustomerFileList.Free;
  fLoginOrgItemOtherList.Free;
  fLoginOrgItemDataList.Free;
  fLoginOrgMsunitList.Free;
  fLoginOrgPackageTypeList.Free;
  fLoginOrgRanksList.Free;
  fLoginOrgChargeItemList.Free;
  fLoginOrgInvoiceTypeList.Free;
  fLoginOrgPaywayList.Free;
  fLoginOrgShipTypeList.Free;

  fOrderList.Free;

  fOrderService.Free;
  fOrderDtlService.Free;
  fCustomerFileService.Free;
  fItemOtherService.Free;
  fItemDataService.Free;
  fMsunitService.Free;
  fPackageTypeService.Free;
  fRanksService.Free;
  fChargeItemService.Free;
  fInvoiceTypeService.Free;
  fPaywayService.Free;
  fShipTypeService.Free;

  inherited Destroy;
end;

function TFrame_smOrderList.InitSeachPanel(out oError:string):Boolean;
begin
  cxLookupComboBox_Customer.Properties.ListSource:=Ds_Customer;
  with cxLookupComboBox_Customer.Properties.ListColumns.Add  do
  begin
    Caption:='客户编号';
    FieldName:=TCmCustomerFileModel.cCustomerIdKey;
    MinWidth:=100;
    Width:=100;
  end;
  with cxLookupComboBox_Customer.Properties.ListColumns.Add  do
  begin
    Caption:='客户名称';
    FieldName:=TCmCustomerFileModel.cCustomerFullNameKey;
    MinWidth:=100;
    Width:=300;
  end;
  with cxLookupComboBox_Customer.Properties.ListColumns.Add  do
  begin
    Caption:='客户名拼音';
    FieldName:='PinYin';
    MinWidth:=0;
    Width:=0;
  end;
  cxLookupComboBox_Customer.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsEditFixedList;
  cxLookupComboBox_Customer.Properties.DropDownAutoSize:=True;             //设置下拉列表为自适应宽度
//      cxLookupComboBox_Customer.Properties.ListFieldNames:=;                 //设置下拉列表需要显示的列
  cxLookupComboBox_Customer.Properties.KeyFieldNames:=TCmCustomerFileModel.cCustomerIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_Customer.Properties.ListFieldIndex:=1;                  //设置返回输入框中的值，用EditText取值

  with cxLookupComboBox_InvoiceType.Properties.ListColumns.Add  do
  begin
    Caption:='发票类型';
    FieldName:=TSmInvoiceTypeModel.cInvoiceTypeNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_InvoiceType.Properties.ListSource:=Ds_InvoiceType;
  cxLookupComboBox_InvoiceType.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_InvoiceType.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_InvoiceType.Properties.ListFieldNames:=TSmInvoiceTypeModel.cInvoiceTypeNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_InvoiceType.Properties.KeyFieldNames:=TSmInvoiceTypeModel.cInvoiceTypeIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_InvoiceType.Properties.ListFieldIndex:=0;                                             //设置返回输入框中的值，用EditText取值

  with cxLookupComboBox_Payway.Properties.ListColumns.Add  do
  begin
    Caption:='支付方式';
    FieldName:=TSmPaywayModel.cPaywayNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_Payway.Properties.ListSource:=Ds_Payway;
  cxLookupComboBox_Payway.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_Payway.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_Payway.Properties.ListFieldNames:=TSmPaywayModel.cPaywayNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_Payway.Properties.KeyFieldNames:=TSmPaywayModel.cPaywayIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_Payway.Properties.ListFieldIndex:=0;

  with cxLookupComboBox_ShipType.Properties.ListColumns.Add  do
  begin
    Caption:='运输方式';
    FieldName:=TSmShipTypeModel.cShipTypeNameKey;
    MinWidth:=100;
    Width:=100;
  end;
  cxLookupComboBox_ShipType.Properties.ListSource:=Ds_ShipType;
  cxLookupComboBox_ShipType.Properties.DropDownListStyle:=TcxEditDropDownListStyle.lsFixedList;
  cxLookupComboBox_ShipType.Properties.DropDownAutoSize:=True;                                        //设置下拉列表为自适应宽度
//    cxLookupComboBox_ShipType.Properties.ListFieldNames:=TSmShipTypeModel.cShipTypeNameKey;     //设置下拉列表需要显示的列
  cxLookupComboBox_ShipType.Properties.KeyFieldNames:=TSmShipTypeModel.cShipTypeIdKey;            //设置返回的字段名，用EditValue取值
  cxLookupComboBox_ShipType.Properties.ListFieldIndex:=0;

  Result:=True;
end;

function TFrame_smOrderList.InitOrderList(out oError:string):Boolean;
var
  Column:TColumnEh;
  ItemKey:string;
begin
  DBGridEh_OrderList.DataSource:=Ds_OrderMst;
  DBGridEh_OrderList.DataSource.DataSet:=nil;
  DBGridEh_OrderList.DataSource.OnDataChange:=Ds_OrderMstDataChange;
  DBGridEh_OrderList.AutoFitColWidths:=False;
  DBGridEh_OrderList.Options:=DBGridEh_OrderList.Options-[dgEditing];
  DBGridEh_OrderList.Options:=DBGridEh_OrderList.Options+[dgRowSelect,dgMultiSelect];
  with DBGridEh_OrderList do
  begin
    ColumnDefValues.Title.Alignment:=taCenter;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='订单号';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cOrderIdKey;
    TDBGridColumnEh(Column).Width:=200;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='客户名称';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cCustomerIdKey;
    TDBGridColumnEh(Column).Width:=300;
    for ItemKey in fLoginOrgCustomerFileList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgCustomerFileList.Items[ItemKey].CustomerId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgCustomerFileList.Items[ItemKey].CustomerFullName);
    end;


    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='订单金额';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cOrderAmountKey;
    TDBGridColumnEh(Column).DisplayFormat:='¥0.00';
    TDBGridColumnEh(Column).Width:=100;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='订单状态';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cOrderStatusKey;
    TDBGridColumnEh(Column).Width:=100;
    TDBGridColumnEh(Column).KeyList.Add('0');
    TDBGridColumnEh(Column).KeyList.Add('1');
    TDBGridColumnEh(Column).KeyList.Add('2');
    TDBGridColumnEh(Column).KeyList.Add('3');
    TDBGridColumnEh(Column).PickList.Add('未审核');
    TDBGridColumnEh(Column).PickList.Add('审核中');
    TDBGridColumnEh(Column).PickList.Add('已审核');
    TDBGridColumnEh(Column).PickList.Add('已作废');

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='发货状态';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cOrderSendStatusKey;
    TDBGridColumnEh(Column).Width:=100;
    TDBGridColumnEh(Column).KeyList.Add('0');
    TDBGridColumnEh(Column).KeyList.Add('1');
    TDBGridColumnEh(Column).KeyList.Add('2');
    TDBGridColumnEh(Column).PickList.Add('未发货');
    TDBGridColumnEh(Column).PickList.Add('部分发货');
    TDBGridColumnEh(Column).PickList.Add('已发货');

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='结算状态';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cOrderSettlementStatusKey;
    TDBGridColumnEh(Column).Width:=100;
    TDBGridColumnEh(Column).KeyList.Add('0');
    TDBGridColumnEh(Column).KeyList.Add('1');
    TDBGridColumnEh(Column).KeyList.Add('2');
    TDBGridColumnEh(Column).PickList.Add('未结算');
    TDBGridColumnEh(Column).PickList.Add('部分结算');
    TDBGridColumnEh(Column).PickList.Add('已结算');

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='发票类型';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cInvoiceTypeIdKey;
    TDBGridColumnEh(Column).Width:=100;
    for ItemKey in fLoginOrgInvoiceTypeList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgInvoiceTypeList.Items[ItemKey].InvoiceTypeId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgInvoiceTypeList.Items[ItemKey].InvoiceTypeName);
    end;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='支付方式';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cPaywayIdKey;
    TDBGridColumnEh(Column).Width:=100;
    for ItemKey in fLoginOrgPaywayList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgPaywayList.Items[ItemKey].PaywayId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgPaywayList.Items[ItemKey].PaywayName);
    end;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='运输方式';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cShipTypeIdKey;
    TDBGridColumnEh(Column).Width:=100;
    for ItemKey in fLoginOrgShipTypeList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgShipTypeList.Items[ItemKey].ShipTypeId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgShipTypeList.Items[ItemKey].ShipTypeName);
    end;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='开单人';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cRecordPsnKey;
    TDBGridColumnEh(Column).Width:=100;
    for ItemKey in fLoginOrgOwnAllUserList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgOwnAllUserList.Items[ItemKey].UserId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgOwnAllUserList.Items[ItemKey].UserName);
    end;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='开单日期';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cRecordDateKey;
    TDBGridColumnEh(Column).DisplayFormat:='yyyy-MM-dd hh:mm:ss';
    TDBGridColumnEh(Column).Width:=200;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='备注';
    TDBGridColumnEh(Column).FieldName:=TSmOrderMstModel.cOrderRemarkKey;
    TDBGridColumnEh(Column).Width:=200;

    RzPanel_OrderDetail.Parent:=RowDetailPanelControl;
    RzPanel_OrderDetail.Align:=alClient;
    RowDetailPanel.Active:=True;
    RowDetailPanel.Height:=150;
    RowDetailPanel.Width:=1000;

    FrozenCols:=4;
  end;

  DBGridEh_OrderDetail.DataSource:=Ds_OrderDtl;
  DBGridEh_OrderDetail.DataSource.DataSet:=nil;
  DBGridEh_OrderDetail.AutoFitColWidths:=False;
  DBGridEh_OrderDetail.Options:=DBGridEh_OrderDetail.Options-[dgEditing];
  DBGridEh_OrderDetail.Options:=DBGridEh_OrderDetail.Options+[dgRowSelect,dgMultiSelect];
  with DBGridEh_OrderDetail do
  begin
    ColumnDefValues.Title.Alignment:=taCenter;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='物料编码';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cItemNoKey;
    TDBGridColumnEh(Column).Width:=150;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='物料名称';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cItemNameKey;
    TDBGridColumnEh(Column).Width:=200;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='规格';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cItemSpecKey;
    TDBGridColumnEh(Column).Width:=100;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='封装类型';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cPackageTypeIdKey;
    TDBGridColumnEh(Column).Width:=100;
    for ItemKey in fLoginOrgPackageTypeList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgPackageTypeList.Items[ItemKey].PackageTypeId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgPackageTypeList.Items[ItemKey].PackageTypeName);
    end;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='产品等级';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cRanksIdKey;
    TDBGridColumnEh(Column).Width:=100;
    for ItemKey in fLoginOrgRanksList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgRanksList.Items[ItemKey].RanksId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgRanksList.Items[ItemKey].RanksName);
    end;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='单位';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cMsunitIdKey;
    TDBGridColumnEh(Column).Width:=80;
    for ItemKey in fLoginOrgMsunitList.SortKeys do
    begin
      TDBGridColumnEh(Column).KeyList.Add(fLoginOrgMsunitList.Items[ItemKey].MsunitId);
      TDBGridColumnEh(Column).PickList.Add(fLoginOrgMsunitList.Items[ItemKey].MsunitName);
    end;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='数量';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cOrderDtlQtyKey;
    TDBGridColumnEh(Column).DisplayFormat:='0.000';
    TDBGridColumnEh(Column).Width:=100;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='单价';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cOrderDtlPriceKey;
    TDBGridColumnEh(Column).DisplayFormat:='¥0.00';
    TDBGridColumnEh(Column).Width:=100;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='金额';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cOrderDtlAmountKey;
    TDBGridColumnEh(Column).DisplayFormat:='¥0.00';
    TDBGridColumnEh(Column).Width:=100;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='已发货数量';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cOrderDtlSendQtyKey;
    TDBGridColumnEh(Column).DisplayFormat:='0.000';
    TDBGridColumnEh(Column).Width:=100;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='已结算金额';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cOrderDtlSettlementAmountKey;
    TDBGridColumnEh(Column).DisplayFormat:='¥0.00';
    TDBGridColumnEh(Column).Width:=100;

    Column:=Columns.Add;
    TDBGridColumnEh(Column).Alignment:=taCenter;
    TDBGridColumnEh(Column).Title.Caption:='备注';
    TDBGridColumnEh(Column).FieldName:=TSmOrderDtlModel.cOrderDtlRemarkKey;
    TDBGridColumnEh(Column).Width:=200;

    FrozenCols:=4;
  end;

  Result:=True;
end;

procedure TFrame_smOrderList.RefreshComponent(const iItem:TSmOrder; const iRunMode:eRunMode);
begin

end;

procedure TFrame_smOrderList.RefreshButton(const iItem:TSmOrder; const iRunMode:eRunMode);
begin

end;

procedure TFrame_smOrderList.RefreshFrame(const iItem:TSmOrder; const iRunMode:eRunMode);
begin
  RefreshComponent(iItem,iRunMode);
  RefreshButton(iItem,iRunMode);
  Obj2Frame(iItem,iRunMode);
end;

procedure TFrame_smOrderList.SkPages1PageChange(Sender: TObject; CurrPage,
  PageSize: Integer; var TotalRecordCount: Integer; var IsSuccess: Boolean;
  var ErrorCode, ErrorText: string);
var
  FieldItem:TStringField;
  OrderBy,Error:string;
  Condition,OrderKey:string;
  OrderItem:TSmOrder;
begin
  inherited;
  DBGridEh_OrderList.DataSource.DataSet:=nil;
  DBGridEh_OrderDetail.DataSource.DataSet:=nil;
  Condition:=Format(string('%s=''%s'' '),[TSmOrderMstModel.cOrgIdKey,fLoginOrg.OrgId]);
  OrderBy:=Format(string('%s DESC'),[TSmOrderMstModel.cRecordDateKey]);
  if fOrderService.GetCDSModel(Cds_OrderMst,Error) and
    fOrderDtlService.GetCDSModel(Cds_OrderDtl,Error) and
    fOrderService.LoadByPageQuery(fOrderList,CurrPage,PageSize,Condition,OrderBy,TotalRecordCount,Error) then
  begin
    for OrderKey in fOrderList.SortKeys do
    begin
      OrderItem:=fOrderList.Items[OrderKey];
      if OrderItem<>nil then
      begin

        if OrderItem.SaveToCDS(Cds_OrderMst,Error) and
          OrderItem.OrderDetailList.SaveToCDS(Cds_OrderDtl,Error) then
        begin

        end else begin
          IsSuccess:=False;
          ErrorText:=Error;
          Exit;
        end;
      end;
    end;
    DBGridEh_OrderList.DataSource.DataSet:=Cds_OrderMst;
    DBGridEh_OrderDetail.DataSource.DataSet:=Cds_OrderDtl;
  end;
end;

procedure TFrame_smOrderList.Action_AddExecute(Sender: TObject);
var
  ParamJson:TQJson;
begin
  inherited;
  ParamJson:=TQJson.Create;
  try
    ParamJson.Add('OrderListManageKey').AsString:=fManageKey;
    AddTabPage('smOrderManage',rmNew,'销售订单',EmptyStr,ParamJson.AsString);
  finally
    ParamJson.Free;
  end;
end;

procedure TFrame_smOrderList.Action_DeleteExecute(Sender: TObject);
var
  Error:string;
begin
  inherited;
  if fCurrOrder<>nil then
  begin
    if fOrderService.Delete(fCurrOrder,Error) then
    begin
      ShowMessage('删除成功');
      SkPages1.Execute;
    end else begin
      ShowMessage('删除失败');
    end;
  end;
end;

procedure TFrame_smOrderList.Action_EditExecute(Sender: TObject);
var
  ParamJson:TQJson;
begin
  inherited;
  ParamJson:=TQJson.Create;
  try
    ParamJson.Add('OrderListManageKey').AsString:=fManageKey;
    AddTabPage('smOrderManage',rmEdit,'销售订单',fCurrOrder.OrderId,ParamJson.AsString);
  finally
    ParamJson.Free;
  end;
end;

procedure TFrame_smOrderList.Action_ViewExecute(Sender: TObject);
var
  ParamJson:TQJson;
begin
  inherited;
  ParamJson:=TQJson.Create;
  try
    ParamJson.Add('OrderListManageKey').AsString:=fManageKey;
    AddTabPage('smOrderManage',rmView,'销售订单',Cds_OrderMst.FieldByName(TSmOrderMstModel.cOrderIdKey).AsString,ParamJson.AsString);
  finally
    ParamJson.Free;
  end;
end;

procedure TFrame_smOrderList.Ds_OrderMstDataChange(Sender: TObject; Field: TField);
var
  CurrOrderId:string;
begin
  inherited;
  CurrOrderId:=Cds_OrderMst.FieldByName(TSmOrderMstModel.cOrderIdKey).AsString;
  Cds_OrderDtl.Filter:=Format(string('%s=''%s'''),[TSmOrderDtlModel.cOrderIdKey,CurrOrderId]);
  Cds_OrderDtl.Filtered:=True;
  if fOrderList.IsExist(CurrOrderId) then
  begin
    fCurrOrder:=fOrderList.Items[CurrOrderId];
  end else begin
    fCurrOrder:=nil;
  end;
  RefreshFrame(fCurrOrder,fRunMode);
end;

procedure TFrame_smOrderList.Obj2Frame(const iItem:TSmOrder; const iRunMode:eRunMode);
begin
  
end;

procedure TFrame_smOrderList.Frame2Obj(const iItem:TSmOrder; const iRunMode:eRunMode);
begin

end;

procedure TFrame_smOrderList.InitFrame(const iManageKey:string; const iRunMode:eRunMode; const iBillNo:string; const iParamStr:string);
var
  PiniYinStr,Key,Ids,Condition,Error:string;
begin
  inherited InitFrame(iManageKey,iRunMode,iBillNo,iParamStr);

  if fCustomerFileService.LoadAll(LoginOrg.OrgId,fLoginOrgCustomerFileList,Error) and
    fCustomerFileService.GetCDSModel(Cds_Customer,Error,eDBLoadMode.dblmView) and
    fLoginOrgCustomerFileList.SaveToCDS(Cds_Customer,Error) then
  begin
    Cds_Customer.First;
    while not Cds_Customer.Eof do
    begin
      //获取每条记录的拼音缩写
      PiniYinStr:=GetStrPYIndex(Cds_Customer.FieldByName(TCmCustomerFileModel.cCustomerFullNameKey).AsString
                      +'|'+Cds_Customer.FieldByName(TCmCustomerFileModel.cCustomerSimpleNameKey).AsString);
      Cds_Customer.Edit;
      Cds_Customer.FieldByName('PinYin').AsString:=PiniYinStr;
      Cds_Customer.Post;
      Cds_Customer.Next;
    end;
    Ds_Customer.DataSet:=Cds_Customer;
  end else begin
    Exit;
    ShowMessage('装载客户基础数据失败!');
  end;

  if fItemOtherService.LoadAll(LoginOrg.OrgId,fLoginOrgItemOtherList,Error) and
    fItemOtherService.GetCDSModel(Cds_ItemOther,Error) and
    fLoginOrgItemOtherList.SaveToCDS(Cds_ItemOther,Error) then
  begin
    Ds_ItemOther.DataSet:=Cds_ItemOther;
    Ids:=EmptyStr;
    for Key in fLoginOrgItemOtherList.SortKeys do
    begin
      Ids:=''''+fLoginOrgItemOtherList.Items[Key].ItemNo+''',';
    end;
    if not SameStr(Trim(Ids),EmptyStr) then
    begin
      Ids:=IfThen(Length(Ids)>0,LeftStr(Ids,Length(Ids)-1),EmptyStr);
      Condition:=Format(string('%s IN (%s)'),[TPmItemDataModel.cItemNoKey,Ids]);
      if fItemDataService.LoadByCondition(fLoginOrgItemDataList,Condition,Error) and
        fItemDataService.GetCDSModel(Cds_ItemData,Error) and
        fLoginOrgItemDataList.SaveToCDS(Cds_ItemData,Error) then
      begin
        Ds_ItemData.DataSet:=Cds_ItemData;
      end else begin
        Exit;
        ShowMessage('装载物料主文件失败!');
      end;
    end;
  end else begin
    Exit;
    ShowMessage('装载本组织物料失败!');
  end;

  if fMsunitService.LoadAll(fLoginOrgMsunitList,Error) and
    fMsunitService.GetCDSModel(Cds_Msunit,Error) and
    fLoginOrgMsunitList.SaveToCDS(Cds_Msunit,Error) then
  begin
    Ds_Msunit.DataSet:=Cds_Msunit;
  end else begin
    Exit;
    ShowMessage('装载单位数据失败!');
  end;

  if fPackageTypeService.LoadAll(fLoginOrgPackageTypeList,Error) and
    fPackageTypeService.GetCDSModel(Cds_PackageType,Error) and
    fLoginOrgPackageTypeList.SaveToCDS(Cds_PackageType,Error) then
  begin
    Ds_PackageType.DataSet:=Cds_PackageType;
  end else begin
    Exit;
    ShowMessage('装载封装类型数据失败!');
  end;

  if fRanksService.LoadAll(fLoginOrgRanksList,Error) and
    fRanksService.GetCDSModel(Cds_Ranks,Error) and
    fLoginOrgRanksList.SaveToCDS(Cds_Ranks,Error) then
  begin
    Ds_Ranks.DataSet:=Cds_Ranks;
  end else begin
    Exit;
    ShowMessage('装载物料等级数据失败!');
  end;

  if fChargeItemService.LoadAll(fLoginOrgChargeItemList,Error) and
    fChargeItemService.GetCDSModel(Cds_ChargeItem,Error) and
    fLoginOrgChargeItemList.SaveToCDS(Cds_ChargeItem,Error) then
  begin
    Ds_ChargeItem.DataSet:=Cds_ChargeItem;
  end else begin
    Exit;
    ShowMessage('装载收费项目数据失败!');
  end;

  if fInvoiceTypeService.LoadAll(fLoginOrgInvoiceTypeList,Error) and
    fInvoiceTypeService.GetCDSModel(Cds_InvoiceType,Error) and
    fLoginOrgInvoiceTypeList.SaveToCDS(Cds_InvoiceType,Error) then
  begin
    Ds_InvoiceType.DataSet:=Cds_InvoiceType;
  end else begin
    Exit;
    ShowMessage('装载收费项目数据失败!');
  end;

  if fPaywayService.LoadAll(fLoginOrgPaywayList,Error) and
    fPaywayService.GetCDSModel(Cds_Payway,Error) and
    fLoginOrgPaywayList.SaveToCDS(Cds_Payway,Error) then
  begin
    Ds_Payway.DataSet:=Cds_Payway;
  end else begin
    Exit;
    ShowMessage('装载支付方式数据失败!');
  end;

  if fShipTypeService.LoadAll(fLoginOrgShipTypeList,Error) and
    fShipTypeService.GetCDSModel(Cds_ShipType,Error) and
    fLoginOrgShipTypeList.SaveToCDS(Cds_ShipType,Error) then
  begin
    Ds_ShipType.DataSet:=Cds_ShipType;
  end else begin
    Exit;
    ShowMessage('装载运输方式数据失败!');
  end;

  if fOrderDtlService.GetCDSModel(Cds_OrderDtl,Error) then
  begin
    Ds_OrderDtl.DataSet:=Cds_OrderDtl;
  end else begin
    Exit;
    ShowMessage('装载订单明细数据失败!');
  end;

  if InitSeachPanel(Error) and
    InitOrderList(Error) then
  begin
    SkPages1.Execute;
  end else begin
    ShowMessage(Format(string('初始化界面失败! 错误内容:%s'),[Error]));
  end;
end;

end.
